package com.ytjj.qmyx.admin.controller.imController;


import com.ytjj.common.api.CommonResult;
import com.ytjj.qmyx.admin.annotation.OperLog;
import com.ytjj.qmyx.admin.constants.SysLogConstants;
import com.ytjj.qmyx.admin.model.ImKeywords;
import com.ytjj.qmyx.admin.model.ImLeaveMsg;
import com.ytjj.qmyx.admin.model.vo.ImLeaveMsgVO;
import com.ytjj.qmyx.admin.service.imService.ImKeywordsService;
import com.ytjj.qmyx.admin.service.imService.ImLeaveMsgService;
import com.ytjj.qmyx.admin.utils.UserUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * IM敏感词相关功能
 */
@RestController
@RequestMapping("/imKeyWords")
@Slf4j
public class ImKeyWordsController {

    @Autowired
    private ImKeywordsService imKeywordsService;

    /**
     * 查询敏感词列表
     * @param imKeywords
     * @return
     */
    @PostMapping("/findList")
    public CommonResult findList(@RequestBody ImKeywords imKeywords){
        return CommonResult.success(imKeywordsService.findList(imKeywords));
    }


    /**
     * 保存
     * @param
     * @return
     */
    @PostMapping("/save")
    @OperLog(operModule = "IM敏感词", operDesc = "保存", operType = SysLogConstants.OPERATION_ADD)
    public CommonResult save(@RequestBody ImKeywords imKeywords){
        try {
            Assert.isTrue(imKeywords!=null && StringUtils.isNotBlank(imKeywords.getKeyword()),"敏感词不能为空");
            Assert.notNull(imKeywords.getType(),"type不能为空");
            imKeywords.setCreateUserid(UserUtil.getAdminId());
            imKeywordsService.add(imKeywords);
        } catch (Exception e) {
            log.error("IM敏感词保存出错:{}",e.getMessage());
            return CommonResult.failed(e.getMessage());
        }
        return CommonResult.success();
    }

    /**
     * 修改
     * @param imKeywords
     * @return
     */
    @PostMapping("/update")
    @OperLog(operModule = "IM敏感词", operDesc = "修改", operType = SysLogConstants.OPERATION_UPDATE)
    public CommonResult update(@RequestBody ImKeywords imKeywords){
        try {
            Assert.isTrue(imKeywords!=null && imKeywords.getId()!=null,"id不能为空");
            imKeywords.setUpdateUserid(UserUtil.getAdminId());
            imKeywordsService.update(imKeywords);
        } catch (Exception e) {
            log.error("IM敏感词修改出错:{}",e.getMessage());
            return CommonResult.failed(e.getMessage());
        }
        return CommonResult.success();
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @PostMapping("/delete")
    @OperLog(operModule = "IM敏感词", operDesc = "删除", operType = SysLogConstants.OPERATION_DELETE)
    public CommonResult delete(Integer id){
        try {
            Assert.isTrue(id!=null,"id不能为空");
            imKeywordsService.delete(id);
        } catch (Exception e) {
            log.error("IM敏感词删除出错:{}",e.getMessage());
            return CommonResult.failed(e.getMessage());
        }
        return CommonResult.success();
    }

}
